package com.gyj.user.service.impl;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.gyj.user.model.User;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @ClassName: UserRedis
 * @Description: redis 提供5种数据类型的操作
 * String ,hash ,list , set , zset
 */
@Repository
public class UserRedis {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Autowired
    private StringRedisTemplate stringRedisTemplate; //public class StringRedisTemplate extends RedisTemplate<String, String>

    /**
     * 添加数据到redis, key-value形式
     * @return
     */
    public String addUser(String key, Long time, User user) {
        Gson gson = new Gson();
        redisTemplate.opsForValue().set(key, gson.toJson(user), time, TimeUnit.MINUTES);
        //stringRedisTemplate.opsForValue().set(key, gson.toJson(user), time, TimeUnit.MINUTES);

        return "success";
    }
    public String addUserByStringRedis(String key, User user) {
        Gson gson = new Gson();
        stringRedisTemplate.opsForValue().set(key, gson.toJson(user));

        return "success";
    }

    /**
     * 添加多个数据到redis, key-value形式
     * @return
     */
    public String addUserList(String key, Long time, List<User> userList) {
        Gson gson = new Gson();
        redisTemplate.opsForValue().set(key, gson.toJson(userList), time, TimeUnit.MINUTES);

        return "success";
    }
    public String addUserListByStringRedis(String key, List<User> userList) {
        Gson gson = new Gson();
        redisTemplate.opsForValue().set(key, gson.toJson(userList));

        return "success";
    }

    /**
     * 根据key值获取user对象
     * @param key
     * @return
     */
    public User getUserByKey(String key) {
        Gson gson = new Gson();
        User user = null;
        String userJson = redisTemplate.opsForValue().get(key);
        if (StringUtils.isNotEmpty(userJson)) {
            user = gson.fromJson(userJson, User.class);
        }

        return user;
    }

    /**
     * 根据key值获取多个user对象
     * @param key
     * @return
     */
    public List<User> getUserListByKey(String key) {
        Gson gson = new Gson();
        List<User> userList = null;
        String userJson = redisTemplate.opsForValue().get(key);
        if (StringUtils.isNotEmpty(userJson)) {
            userList = gson.fromJson(userJson, new TypeToken<List<User>>() {}.getType());
        }

        return userList;
    }

    /**
     * 根据Key删除索引value
     * @param key
     */
    public void deleteByKey(String key) {
        //下面这两中方式都可以
        //redisTemplate.opsForValue().getOperations().delete(key);
        stringRedisTemplate.opsForValue().getOperations().delete(key);
    }

    /**
     * 判断缓存中是否有对应的value
     * @param key
     */
    public boolean exists(final String key) {
        return redisTemplate.hasKey(key);
    }
}
